package com.trm.login.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.trm.login.dao.LoginDao;
import com.trm.role.dao.RoleDao;

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String methodName = request.getParameter("action");
        try {
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this.getClass().newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录
     * 
     * @param request
     * @param response
     */
    public void login(HttpServletRequest request, HttpServletResponse response) {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        LoginDao dao = new LoginDao();
        Map<String, Object> user = dao.login(account, password);
        JSONObject json = new JSONObject();
        PrintWriter out = null;
        if (user != null) {
            String roleId = user.get("role").toString();
            RoleDao roleDao = new RoleDao();
            Map<String, Object> role = roleDao.getRoleById(roleId);
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            session.setAttribute("role", role);
            String userAgent = request.getHeader("user-agent");
            // System.out.println("登录设备---------------------");
            // System.out.println(userAgent);
            String url = "IndexServlet";
            if (userAgent.toLowerCase().contains("android") || (userAgent.toLowerCase().contains("iphone"))
                    || (userAgent.toLowerCase().contains("phone"))) {
                url = "IndexServlet?type=mobile";
                session.setAttribute("loginType", "mobile");
            } else {
                session.setAttribute("loginType", "web");
            }
            /*
             * try { response.sendRedirect(url); } catch (IOException e) {
             * e.printStackTrace(); }
             */
            json.put("result", "success");
            json.put("url", url);
        } else {
            json.put("result", "error");
        }
        try {
            out = response.getWriter();
            out.print(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    /**
     * 退出
     * 
     * @param request
     * @param response
     */
    public void exit(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.invalidate();
        try {
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
